package entities;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Text;

import models.Domain;

public class DomainEntity {
	public static Entity convert(Domain mDomain) {
		Entity eDomain = new Entity("Domain", mDomain.getName());
		
		//Set name
		eDomain.setProperty("name", mDomain.getName());
		//Set title
		eDomain.setProperty("title", new Text(mDomain.getTitle()));
		//Set keywords
		eDomain.setProperty("keywords", new Text(mDomain.getKeywords()));
		//Set popularKeywords
		eDomain.setProperty("popularKeywords", new Text(mDomain.getPopularKeywords()));
		//Set description
		eDomain.setProperty("description", new Text(mDomain.getDescription()));
		//Set visitorPerDay
		eDomain.setProperty("visitorPerDay", new Text(Integer.toString(mDomain.getVisitorPerDay())));
		
		return eDomain;
	}
	
	public static void addEntity(Entity domain) {
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.put(domain);
	}
	
	public static Entity getEntity(String domainName) {
		Key k = KeyFactory.createKey("Domain", domainName);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		Entity domain = null;
		
		try {
			domain  =  datastore.get(k);
		} catch (EntityNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return domain;
	}
	
	public static void deleteEntity(String domainName) {
		Key k = KeyFactory.createKey("Domain", domainName);
		DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
		datastore.delete(k);
	}
	
	public static void main(String[] args) {
		Domain domain = new Domain();
	}
}
